package com.sctu.EMSApp;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.bean.JsonBean;
import com.sctu.bean.JsonListBean;
import com.sctu.bean.User;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;
import com.sctu.util.SharePreferenceUtil;
import org.json.JSONException;


import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhu on 2016/1/4.
 */
public class LoginActivity extends FragmentActivity implements View.OnClickListener{
    private ImageView login,register,forget;
    private EditText usernameEdit,passwordEdit;
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setView();
        setEven();

    }

    private void setEven() {
        login.setOnClickListener(this);
        register.setOnClickListener(this);
        forget.setOnClickListener(this);


    }

    private void setView() {

        login = (ImageView) findViewById(R.id.my_login_botton_id);
        register = (ImageView) findViewById(R.id.my_register_botton_id);
        forget = (ImageView) findViewById(R.id.my_forget_botton_id);
        usernameEdit = (EditText) findViewById(R.id.login_username);
        passwordEdit = (EditText) findViewById(R.id.login_password);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()){
            case R.id.my_login_botton_id:
                HashMap<String,String> loginMsg = new HashMap<String, String>();
                loginMsg.put("username",usernameEdit.getText().toString());
                loginMsg.put("password",passwordEdit.getText().toString());
                GetVolleyHttp.getInstance(this).getStringPOSTJsonData(UrlConfig.URL_USER_LOGIN, loginMsg,listener,errorListener);
                break;
            case R.id.my_register_botton_id:
                intent.setClass(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.my_forget_botton_id:
                break;
        }
    }
    Response.Listener<String> listener = new Response.Listener<String>() {

        @Override
        public void onResponse(String s) {
            Log.e("heheda", s);
//            SharePreferenceUtil.getInstance(LoginActivity.this).getKeyData("userId");
            User userJsonListBean= JSON.parseObject(s,User.class);
            if (userJsonListBean.getCode() == 0){
                User.Result result = userJsonListBean.getResult().get(0);
                SharePreferenceUtil.getInstance(LoginActivity.this).setKeyData("username",result.getUsername());
                SharePreferenceUtil.getInstance(LoginActivity.this).setKeyData("islogin","1");
                SharePreferenceUtil.getInstance(LoginActivity.this).setKeyData("userId",result.getUid());
                finish();
            }else {
                Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_LONG).show();
            }


        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Toast.makeText(LoginActivity.this, "网络出错", Toast.LENGTH_LONG).show();
        }
    };
}
